﻿
#region Usings
using System;
using System.IO;




#endregion

namespace PolyMorph.LevelEditor
{
    public static class FileHandler
    {
        static string workingDir = Directory.GetCurrentDirectory();
        public static String Load(String LevelName)
        {

            if (LevelName == "No Levels In Folder")
                return "";
            if (LevelName == "")
                LevelName =workingDir+ @"\Levels\Level0.txt";
            else
                LevelName = workingDir + @"\Levels\" + LevelName;
            if (!LevelName.EndsWith(".txt"))
                LevelName += ".txt";

            try
            {
                StreamReader SR = new StreamReader(LevelName);
                return SR.ReadToEnd();
            }
            catch (Exception)
            {
                throw new Exception("Could Not find Level");
            }
        }

        public static string Save(string LevelData)
        {
            //Saving the Level (in progress)
            int levelCount;
            if(!Directory.Exists(workingDir+@"\Levels"))
                Directory.CreateDirectory(workingDir + @"\Levels");
            levelCount = Directory.GetFiles(workingDir + @"\Levels").Length;


            StreamWriter SW = new StreamWriter(workingDir + @"\Levels\Level" + levelCount + ".txt", true);
            String[] lines = LevelData.Split('\n');
            for (int i = 0; i < lines.Length; i++)
            {
                if (lines[i] != "")
                    SW.WriteLine(lines[i]);
            }

            //Close the file.
            SW.Close();
            return "Level" + levelCount;

        }
    }


}
